import JsEncrypt from 'jsencrypt';
import { Notice } from 'view-design'

const INVALID = /[#\$%\^&\*？?]+/g;
const TEL = /^[1][3,4,5,7,8][0-9]{9}$/
const EMAIL = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
const PASSED = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$/;
const PROJECT_VIEW = /\/project\/\d+(?![\/\d])/;

let noLogin = [
    '/',
    '/home',
    '/login'
]

String.prototype.imgUrl = function() {
    return this.replace(/^(http)[s]*(\:\/\/)/, 'https://images.weserv.nl/?url=')
}

String.prototype.encrypt = function() {
    let encrypt = new JsEncrypt();
    encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCh+vgqXiTPni0eBMFZ9YnrjBcqmvGJKVS6PlIPLA/hzStb1B+nKEYaPi1lDCAZbS1eps/ubbHkSyBniSRNEh/hbZGozjlZ4E4ZZFwhS2wzkjkbWPRCUJyYx0v8ukC4zrO1ab+nGM/t3LgxNwQQFKyLexduFaghruvjxmnwqwPVtwIDAQAB');
    return encrypt.encrypt(this);
}

String.prototype.isEmpty = function() {
    return this.length == 0;
}

String.prototype.isTelephone = function() {
    return TEL.test(this);
}

String.prototype.isInvalid = function() {
    return INVALID.test(this);
}

String.prototype.isPassword = function() {
    return PASSED.test(this);
}

String.prototype.isEmail = function() {
    return EMAIL.test(this);
}

String.prototype.contain = function(subStr) {
    return this.indexOf(subStr) != -1;
}

String.prototype.deepCopy = function(obj) {
    return JSON.parse(JSON.stringify(obj));
}

String.prototype.notifyOK = function() {
    Notice.success({
        desc: this
    })
}

String.prototype.notifyWarn = function() {
    Notice.warning({
        desc: this
    })
}

String.prototype.notifyError = function() {
    Notice.error({
        desc: this
    })
}

String.prototype.requireLess = function() {
    return noLogin.indexOf(this) != -1 || PROJECT_VIEW.test(this)
}

Date.prototype.currentTime = function() {
    let date = new Date();

    let intParser = num => num < 10 ? "0" + num : num;
    
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate(); 
    let hour = date.getHours(); 
    let minute = date.getMinutes();
    let second = date.getSeconds();
    return `${year}-${intParser(month)}-${intParser(day)} ${intParser(hour)}:${intParser(minute)}:${intParser(second)}`;
}

Map.prototype.valueArray = function() {
	let result = [];
	let itr = this.values();
	let node;
	do {
		node = itr.next();
		if (node.value != undefined) {
            result.push(node.value);
        }
	} while(!node.done);
	return result;
}

Map.prototype.keyArray = function() {
	let result = [];
	let itr = this.keys();
	let node;
	do {
		node = itr.next();
		if (node.value != undefined) {
            result.push(node.value);
        }
	} while(!node.done);
	return result;
}

Map.prototype.toObj = function() {
    let obj= Object.create(null);
    for (let [k, v] of this) {
		obj[k] = v;
    }
    return obj;
}

Map.prototype.toMap = function(obj) {
	let map = new Map();
	for (let k of Object.keys(obj)) {
        if (isNaN(k)) {
		    map.set(k, obj[k]);
        } else {
            map.set(Number.parseInt(k), obj[k]);
        }
	}
	return map;
}

const delay = (function () {
    let timer = 0
    return function (callback, ms) {
        clearTimeout(timer)
        timer = setTimeout(callback, ms)
    }
})();

String.prototype.debounce = function(fn, time) {
    delay(fn, time);
}

Array.prototype.quicksort = function(field, method = 'UP') {
    if (this.length <= 1) {
        return this;
    }
    let right = [];
    let left = [];
    let mid = Math.floor(this.length / 2);
    let midItem = this[mid];
    for (let index = 0; index < this.length; index++) {
        if (index == mid) {
            continue;
        }
        if (this[index][field] < midItem[field]) {
            if (method == 'UP') {
                left.push(this[index]);
            } else {
                right.push(this[index]);
            }
        } else if (this[index][field] > this[mid][field]) {
            if (method == 'UP') {
                right.push(this[index]);
            } else {
                left.push(this[index]);
            }
        }
    }
    return left.quicksort(field, method).concat(midItem).concat(right.quicksort(field, method));
}

Array.prototype.contain = function(field, value) {
    if (this.length == 0) {
        return false;
    }
    for (let index = 0; index < this.length; index++) {
        if (this[index][field] == value) {
            return true;
        }
    }
    return false;
}